<?php
include ('./mysqli_connect.php');
include('./Session.php');

if(isset($_REQUEST['ct'])){
    $chattext = mysqli_real_escape_string($dbc, $_REQUEST['ct']);
    $query = "INSERT INTO `chat`(c_mess,chat_time,u_id) VALUES ('{$chattext}',NOW(),15)";
    $result = mysqli_query($dbc, $query);
    $querys = "SELECT c_mess,chat_time,u_id,user_login FROM `chat` INNER JOIN `user` ON u_id = user_id ORDER BY c_id DESC LIMIT 5";
    $results = mysqli_query($dbc, $querys);
    if(mysqli_num_rows($results)>0){
    while($chat = mysqli_fetch_array($results,MYSQLI_ASSOC)){
        echo" <li><div class='chat-info'>
                            <div class='c-user-img'><img src='Images/users.png' alt='user-icon' width='50' height='50'/></div>
                            <div class='c-user-name'>{$chat['user_login']}</div>
                            <div class='c-time'>{$chat['chat_time']}</div>
                            </div>
                            <p class='c-text'>{$chat['c_mess']}</p>
                        </li>";
    }      
    }
}else{
    echo "error";
}
